package pers.vinsonws.jianghu.serverspring.service;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;

/**
 * @auther vinsonws
 * @date 2018/10/29 20:08
 * netty服务启动类
 */
@Service
public class ChatRoomStartService {
    private static final Logger log = LoggerFactory.getLogger(ChatRoomStartService.class);

    @Autowired
    private ServerBootstrap serverBootstrap;

    @Autowired
    private InetSocketAddress tcpSocketAddress;

    ChannelFuture channelFuture;
    @PostConstruct
    public void start() throws InterruptedException {

        channelFuture = serverBootstrap.bind(tcpSocketAddress).sync();
        log.info("server start running in port:" + tcpSocketAddress);
    }
    @PreDestroy
    public void stop() throws InterruptedException {

        channelFuture.channel().closeFuture().sync();
        log.info("server stop! ");
    }


}
